package com.fenghongzhang.day006;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private ViewPager vp;

    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //2初始化
        vp = (ViewPager) findViewById(R.id.vp);

        //3,数据源
        final List<String> list = new ArrayList<>();
        list.add("第一个");
        list.add("第二个");
        list.add("第三个");

        //3 创建适配器
        MyVPAdapter myVPAdapter = new MyVPAdapter(this,list);

        //4设置配置器
        vp.setAdapter(myVPAdapter);

        //设置当前vp要显示的页面
        //vp.setCurrentItem(2);


        //设置动画效果
        vp.setPageTransformer(false,new DepthPageTransformer());


        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            int index = 0;
            @Override
            public void run() {
                //主线程
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                      //  vp.setCurrentItem(index);
                        if(++index > list.size() -1){
                            index = 0;
                        }
                    }
                });

            }
        },0,1000);


        //翻页的监听器
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//                Log.i(TAG, "onPageScrolled: "+positionOffset+positionOffsetPixels);
            }

            //当前页面上上一页不同的时候掉
            @Override
            public void onPageSelected(int position) {
                Log.i(TAG, "onPageSelected: "+position);
                if(position == list.size() -1){
                    timer.cancel();

                    //开启页面跳转的秒数.
                }

            }


            @Override
            public void onPageScrollStateChanged(int state) {
                //三种

            }
        });

    }
}
